from typing import Annotated

from langchain_core.tools import tool
from langchain_tavily.tavily_search import TavilySearch


@tool
def web_search(
    query: Annotated[str, "要联网搜索的问题"], num_results: Annotated[int, "要搜索出的结果数"] = 3
):
    """对于无法回答的问题,尝试使用该联网搜索工具"""
    searcher = TavilySearch(num_results=num_results)

    return searcher.invoke(query)["results"]


if __name__ == "__main__":
    from dotenv import find_dotenv, load_dotenv

    _ = load_dotenv(find_dotenv())
    print(web_search.invoke("如何使用langchain?"))
